*******************************************************************************
* > Loop over datasets
*******************************************************************************

foreach dataset in gallup gss {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_`dataset', clear
	
*******************************************************************************
* Set params based on data set
*******************************************************************************

if "`dataset'" == "gallup" {
	rename conf Y
	local mytitle "Confidence (Gallup)"
	local treated_target "police"
	local xaxis 1990(8)2022
}
if "`dataset'" == "gss" {
	rename nat Y
	local mytitle "Spending (GSS)"
	local treated_target "crimy"
	local xaxis 1982(8)2022	
}

*******************************************************************************
* Make the party differences by opinion object
*******************************************************************************

egen Y_dem = mean(Y) if subgroup == "Democrat", by(target year)
egen Y_rep = mean(Y) if subgroup == "Republican", by(target year)
collapse Y_dem Y_rep, by(target year)
gen Y = Y_dem - Y_rep

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local legend1 legend(off)
local legend2 legend(off)
if "`subgroup'" == "All" {
	local legend1 legend(order(1 "{fontface monospace:{bf:Police}}" ///
		2 "{fontface monospace:{bf:Synthetic Police}}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none)))
	local legend2 legend(order(1 "{fontface monospace:{bf:Difference}}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none)))
	
}

*******************************************************************************
* Make lags
*******************************************************************************

su year
local mymin = r(min)

local lags
levelsof year if year < 2014, local(levels)
foreach i of local levels {
	egen Y`i'm = mean(Y) if year == `i', by(target)
	egen Y`i' = mean(Y`i'm), by(target)
	drop Y`i'm
	local lags `lags' Y`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "`treated_target'"

*******************************************************************************
* Synth
*******************************************************************************

synth Y `lags', trunit(`r(mean)') trperiod(2014) keep(tmp, replace)
use tmp, clear
erase tmp.dta

*******************************************************************************
* Graph
*******************************************************************************

#delimit;

gr tw
	(line _Y_treated _time, col(black) lwid(thick))
	(line _Y_synthetic _time, col(gray) lwid(thick))
	,
		name(g1, replace)
		xline(2014, lpat(dash))
		ylab(, angle(horiz))
		plotregion(style(none))
		ytitle("")
		xtitle("")
		`legend1'	
		xlab(1990(8)2022)		
		;

#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy

gen d = _Y_treated - _Y_synthetic

#delimit;

gr tw
	(line d _time, col(black) lwid(thick))
	(sc d _time, msym(none))
	,
		name(g2, replace)
		yline(0, lpat(shortdash))
		xline(2014, lpat(dash))
		plotregion(style(none))
		ylab(, angle(horiz))
		ytitle("")
		xtitle("")
		xlab(1990(8)2022)		
		`legend2'
		;

#delimit cr

gr combine g1 g2, name(G`dataset', replace) title(`mytitle')

*******************************************************************************
* > Close dataset loop
*******************************************************************************

}

*******************************************************************************
* Combine plots for each dataset
*******************************************************************************

gr combine Ggallup Ggss, rows(2) ysize(4) xsize(5) imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_sc_partydiffs.pdf", replace

*******************************************************************************
* End
*******************************************************************************
